package com.nbpt;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {
    EditText edtUsername,edtPss1,edtPss2,edtPhone;
    String resp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        edtUsername = findViewById(R.id.edtUser);
        edtPss1 = findViewById(R.id.edtPss1);
        edtPss2 = findViewById(R.id.edtPss2);
        edtPhone = findViewById(R.id.edtPhone);
    }

    public void clickback(View view){
        finish();
    }

    public void register(View view){
        String pass1 = edtPss1.getText().toString();
        String pass2 = edtPss1.getText().toString();
        String username = edtUsername.getText().toString();
        String phone = edtPhone.getText().toString();
        if (edtUsername.getText().toString().isEmpty()||
                edtPss1.getText().toString().isEmpty() ||
                edtPss2.getText().toString().isEmpty()||
                edtPhone.getText().toString().isEmpty()){
            Toast.makeText(this, "信息为输入不完整", Toast.LENGTH_SHORT).show();
        }else if(!pass1.equals(pass2)){
            Toast.makeText(this,"两次密码不一致",Toast.LENGTH_SHORT).show();
        }
        else{
            OkHttpClient client = new OkHttpClient();
            MediaType JSON_TYPE = MediaType.parse("application/json;charset=utf-8");
            JSONObject upload  = new JSONObject();
            try {
                upload.put("username",username);
                upload.put("phone",phone);
                upload.put("password",pass1);
                upload.put("email","");
                upload.put("avatar","");
                upload.put("createdAt","");
                upload.put("id","");
                upload.put("nickname","");

            } catch (JSONException e) {
                e.printStackTrace();
            }
                String json = upload.toString();
//            String json = "{\n" +
//                    "\t\"avatar\": \"\",\n" +
//                    "\t\"createdAt\": \"\",\n" +
//                    "\t\"email\": \"\",\n" +
//                    "\t\"id\": 0,\n" +
//                    "\t\"nickname\": \"\",\n" +
//                    "\t\"password\": \"x174825647\",\n" +
//                    "\t\"phone\": \"15557895960\",\n" +
//                    "\t\"username\": \"xsl666\"\n" +
//                    "}";
            RequestBody body = RequestBody.create(JSON_TYPE,json);
            Request request = new Request.Builder()
                    .url("http://192.168.1.7:9020/iot/user/register")
                    .post(body).build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    //网络请求失败
                    Log.e("RegisterActivity",e.getMessage());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    //网络响应
                    resp = response.body().string();
                    Log.e("RegisterActivity",resp);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonObject = new JSONObject(resp);
                                int code = jsonObject.getInt("code");
                                String message = jsonObject.getString("msg");
                                if(code ==1001){
                                    Toast.makeText(RegisterActivity.this, "注册成功",
                                            Toast.LENGTH_SHORT).show();
                                    finish();
                                }else{
                                    Toast.makeText(RegisterActivity.this,message,
                                            Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });


                }
            });



        }
    }
}
